package com.cd.javacxy.module.web.service.impl;

import com.cd.javacxy.common.model.entity.CxySignIn;
import com.cd.javacxy.common.model.entity.SessionUser;
import com.cd.javacxy.common.model.enums.DateTimePatternEnum;
import com.cd.javacxy.common.model.enums.MarkEnum;
import com.cd.javacxy.common.model.query.SignInQuery;
import com.cd.javacxy.common.model.vo.CxyUserVo;
import com.cd.javacxy.common.model.vo.SignInfoVo;
import com.cd.javacxy.common.utils.Constants;
import com.cd.javacxy.common.utils.DateUtil;
import com.cd.javacxy.exception.BussinessException;
import com.cd.javacxy.module.web.dao.CxySignInDao;
import com.cd.javacxy.module.web.service.CxySignInService;
import com.cd.javacxy.module.web.service.CxyUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * CxySignIn的服务接口的实现类
 *
 * @author yanghy
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CxySignInServiceImpl implements CxySignInService {

    private final CxySignInDao cxySignInDao;

    private final CxyUserService userService;

    @Override
    public SignInfoVo findSignInfoByUserId(Integer userId) {
        SignInQuery signInQuery = new SignInQuery();
        SignInfoVo signInfo = new SignInfoVo();
        Date curDate = new Date();
        signInQuery.setCurDate(curDate);
        //查询当前日期签到了多少人
        signInfo.setTodaySignInCount(this.cxySignInDao.selectCount(signInQuery));
        if (null == userId) {
            //没登录,默认没签到，签到天数为0
            signInfo.setHaveSignInToday(Boolean.FALSE);
            signInfo.setUserSignInCount(0);
        } else {
            signInQuery = new SignInQuery();
            CxyUserVo user = this.userService.findUserByUserId(userId);
            signInfo.setMark(user.getMark());
            signInQuery.setUserId(userId);
            //查询该用户总共签到了多少天
            int userSignInCount = this.cxySignInDao.selectCount(signInQuery);
            signInfo.setUserSignInCount(userSignInCount);
            signInQuery.setCurDate(curDate);
            int todayIsSignIn = this.cxySignInDao.selectCount(signInQuery);
            if (todayIsSignIn == 0) {
                //查询该用户当天是否签到
                signInfo.setHaveSignInToday(Boolean.FALSE);
            } else {
                signInfo.setHaveSignInToday(Boolean.TRUE);
            }
            signInfo.setCurDate(DateUtil.format(curDate, DateTimePatternEnum.MM_POINT_DD.getPattern()));
            signInfo.setCurYear(DateUtil.format(curDate, DateTimePatternEnum.YYYY.getPattern()));
            signInfo.setCurMonth(DateUtil.format(curDate, DateTimePatternEnum.MM.getPattern()));
            signInfo.setCurDay(DateUtil.format(curDate, DateTimePatternEnum.dd.getPattern()));

        }
        return signInfo;
    }

    /**
     * 签到是事物，配置开启新事物，并且只有在发生BussinessException才回滚
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = BussinessException.class)
    public CxySignIn doSignIn(SessionUser sessionUser) throws BussinessException {
        Integer userId = sessionUser.getUserId();
        LocalDateTime curDate = LocalDateTime.now();
        SignInQuery signInQuery = new SignInQuery();
        signInQuery.setUserId(userId);
        signInQuery.setCurDate(new Date());
        int todayUserSignInCount = this.cxySignInDao.selectCount(signInQuery);
        if (todayUserSignInCount > 0) {
            throw new BussinessException("今日已经签到");
        }
        CxySignIn signIn = new CxySignIn();
        signIn.setUserId(sessionUser.getUserId());
        signIn.setUserIcon(sessionUser.getUserIcon());
        signIn.setUserName(sessionUser.getUserName());
        signIn.setSignDate(curDate);
        signIn.setSignTime(curDate);
        this.cxySignInDao.insert(signIn);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -6);
        //把当前日期置空，以便查询区间日期是否连续签到
        signInQuery.setCurDate(null);

        signInQuery.setStartDate(calendar.getTime());
        signInQuery.setEndDate(new Date());
        int count = this.cxySignInDao.selectCount(signInQuery);
        int mark;
        if (count >= Constants.CONTINUOUS_SIGNIN) {
            mark = MarkEnum.MARK_SIGN_CONTINUE.getMark();
        } else {
            mark = MarkEnum.MARK_SIGNIN.getMark();
        }
        this.userService.addMark(mark, userId);
        return signIn;
    }
}